home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib_tools / RDFPipe.py < prev   
Encoding:
Python Source  |  2009-02-22  |  2.7 KB  |  85 lines

  1. from pprint import pprint
  2. from rdflib.Namespace import Namespace
  3. from rdflib import plugin,RDF,RDFS,URIRef
  4. from rdflib.store import Store
  5. from rdflib.Graph import Graph
  6. from rdflib.syntax.NamespaceManager import NamespaceManager
  7.  
  8. RDFLIB_CONNECTION=''
  9. RDFLIB_STORE='IOMemory'
  10.  
  11. import getopt, sys
  12.  
  13. def usage():
  14.     print """USAGE: RDFPipe.py [options]
  15.     
  16.     Options:
  17.     
  18.       --stdin                     Parse RDF from STDIN (useful for piping)
  19.       --help                      
  20.       --input-format              Format of the input document(s).  One of:
  21.                                   'xml','trix','n3','nt','rdfa'
  22.       --output                    Format of the final serialized RDF graph.  One of:
  23.                                   'n3','xml','pretty-xml','turtle',or 'nt'
  24.       --ns=prefix=namespaceUri    Register a namespace binding (QName prefix to a 
  25.                                   base URI).  This can be used more than once"""
  26.  
  27. def main():
  28.     try:
  29.         opts, args = getopt.getopt(sys.argv[1:], "", ["output=","ns=","input=","stdin","help","input-format="])
  30.     except getopt.GetoptError, e:
  31.         # print help information and exit:
  32.         print e
  33.         usage()
  34.         sys.exit(2)
  35.  
  36.     factGraphs = []
  37.     factFormat = 'xml'
  38.     useRuleFacts = False
  39.     nsBinds = {
  40.         'rdf' : RDF.RDFNS,
  41.         'rdfs': RDFS.RDFSNS,
  42.         'owl' : "http://www.w3.org/2002/07/owl#",       
  43.         'dc'  : "http://purl.org/dc/elements/1.1/",
  44.         'foaf': "http://xmlns.com/foaf/0.1/",
  45.         'wot' : "http://xmlns.com/wot/0.1/"        
  46.     }
  47.     outMode = 'n3'
  48.     stdIn = False
  49.     if not opts:
  50.         usage()
  51.         sys.exit()        
  52.     for o, a in opts:
  53.         if o == '--input-format':
  54.             factFormat = a
  55.         elif o == '--stdin':
  56.             stdIn = True
  57.         elif o == '--output':
  58.             outMode = a
  59.         elif o == '--ns':            
  60.             pref,nsUri = a.split('=')
  61.             nsBinds[pref]=nsUri
  62.         elif o == "--input":
  63.             factGraphs = a.split(',')
  64.         elif o == "--help":
  65.             usage()
  66.             sys.exit()
  67.         
  68.     store = plugin.get(RDFLIB_STORE,Store)()        
  69.     store.open(RDFLIB_CONNECTION)
  70.     namespace_manager = NamespaceManager(Graph())
  71.     for prefix,uri in nsBinds.items():
  72.         namespace_manager.bind(prefix, uri, override=False)    
  73.     factGraph = Graph(store) 
  74.     factGraph.namespace_manager = namespace_manager
  75.     if factGraphs:
  76.         for fileN in factGraphs:
  77.             factGraph.parse(fileN,format=factFormat)
  78.     if stdIn:
  79.         factGraph.parse(sys.stdin,format=factFormat)
  80.     print factGraph.serialize(destination=None, format=outMode, base=None)
  81.     store.rollback()
  82.  
  83. if __name__ == "__main__":
  84.     main()
  85.